require("View")

LogView = class("LogView", View)

---

function LogView:initialize()

	View.initialize(self)
	
	self.rootWindow = wx.wxTreeCtrl( 
		gView.frame, 
		wx.wxID_ANY,
        wx.wxDefaultPosition, 
        wx.wxSize(-1, -1),
        wx.wxTR_LINES_AT_ROOT + wx.wxTR_HAS_BUTTONS 
    )     
        
	self.rootId = self.rootWindow:AddRoot( "Log" )
	self.previousBeatID = false
	self.hadBoldEntry = false
end

---

function LogView:update()

	if self.previousBeatID then
	
		self.rootWindow:Delete(self.previousBeatID)
		self.previousBeatID = self:addGroup(gLogger.history[gField.beat - 1].entry, self.rootId)
		
		if self.hadBoldEntry then
			self.rootWindow:Expand(self.previousBeatID)
		else
			self.rootWindow:Collapse(self.previousBeatID)
		end
		
		self.hadBoldEntry = false
	end
	
	self.previousBeatID = self:addGroup(gLogger.history[gField.beat].entry, self.rootId)
	self.rootWindow:Expand(self.rootId)
end

---

function LogView:addGroup(group, parentID)

	local groupID
	local willHaveToExpand
	
	for i, entryDescriptor in ipairs(group) do
		
		if i == 1 then
		
			groupID = self.rootWindow:AppendItem(parentID, entryDescriptor.entry )
			willHaveToExpand = not entryDescriptor.collapsedViewByDefault
		else
			if type(entryDescriptor.entry) == "table" then
			
				self:addGroup(entryDescriptor.entry, groupID)
			else
				local entryID = self.rootWindow:AppendItem(groupID, tostring(entryDescriptor.entry) )
				
				if entryDescriptor.bold then
					self.rootWindow:SetItemBold(entryID)
					self.hadBoldEntry = true
				end
				
				self.rootWindow:Expand(entryID)
			end
		end
	end
	
	if willHaveToExpand then
	
		self.rootWindow:Expand(groupID)
		self.rootWindow:EnsureVisible(groupID)
	end
	
	
	return groupID
end
